﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    public class AttachmentDal
    {
        public DapperHelper dapperHelper { get; set; }

        /// <summary>
        /// 添加附件save(TAttachment attachment)
        /// </summary>
        /// <param name="attachmentView"></param>
        /// <returns></returns>
        public bool SaveAttachment(AttachmentView attachmentView) {
            return dapperHelper.ExecueWithoutTransaction<bool>(a => {
                string sql = @"insert into t_attachment(ATTID, ATTTYPE, OUTERID, 
                                                        ATTDIR, ATTSTATE, INTIME, 
                                                        INUSER, MODTIME, MODUSER
                                                         ) values(
                                                        @ATTID, @ATTTYPE, @OUTERID, 
                                                        @ATTDIR,@ATTSTATE, @INTIME, 
                                                        @INUSER,@MODTIME, @MODUSER
                                                        )";
                SqlBuilder sqlbuilder = new SqlBuilder();
                var sqlTemplate = sqlbuilder.AddTemplate(sql);
                a.Execute(sqlTemplate.RawSql,attachmentView);
                return true;
            });
        }

        /// <summary>
        /// 根据outerid获取附件列表
        /// </summary>
        /// <param name="outerid"></param>
        /// <returns></returns>
        public IEnumerable<AttachmentView> FindTAttachments(string outerid)
        {
            return dapperHelper.ExecueWithoutTransaction<IEnumerable<AttachmentView>>(a=> {
                string sql = @"select  ATTID,ATTDIR from t_attachment /**where**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("attstate = 1");
                sqlBuilder.Where("outerid = @outerid", new { outerid = outerid });
                return a.Query<AttachmentView>(sqlTemplate.RawSql, sqlTemplate.Parameters);
               
            });
        }

        /// <summary>
        /// 根据outerid获取附件列表
        /// </summary>
        /// <param name="outerid"></param>
        /// <returns></returns>
        public AttachmentView FindTAttachmentByAttid(string attid)
        {
            return dapperHelper.ExecueWithoutTransaction<AttachmentView>(a => {
                string sql = @"select  * from t_attachment /**where**/";
                SqlBuilder sqlBuilder = new SqlBuilder();
                var sqlTemplate = sqlBuilder.AddTemplate(sql);
                sqlBuilder.Where("attid = @attid", new { attid = attid });
                return a.Query<AttachmentView>(sqlTemplate.RawSql, sqlTemplate.Parameters).First();

            });
        }

       
        

        /// <summary>
        /// 更新附件状态
        /// </summary>
        /// <param name="attid"></param>
        /// <returns></returns>
        public bool ModAttState(string attid) {
            return dapperHelper.ExecueWithoutTransaction<bool>(a => {
                string sql = "update t_attachment set ATTSTATE = 4 where ATTID = '"+attid+"'";
                SqlBuilder sqlbuilder = new SqlBuilder();
                var sqlTemplate = sqlbuilder.AddTemplate(sql);
                a.Execute(sqlTemplate.RawSql);
                return true;
            });
        }

        /// <summary>
        /// 更新附件状态
        /// </summary>
        /// <param name="attid"></param>
        /// <returns></returns>
        public bool ModAttStateByDir(string dir)
        {
            return dapperHelper.ExecueWithoutTransaction<bool>(a => {
                string sql = "update t_attachment set ATTSTATE = 4 where ATTDIR = '" + dir + "'";
                SqlBuilder sqlbuilder = new SqlBuilder();
                var sqlTemplate = sqlbuilder.AddTemplate(sql);
                a.Execute(sqlTemplate.RawSql);
                return true;
            });
        }

    }
}
